﻿using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace AlfarEngine.Utilities
{
    public class Debug
    {
        static private Texture2D dummyTexture;
        static private bool mDebug;
        static private SpriteFont font;

        static internal int numberOfSpriteDraw;

        static public bool debug { get { return mDebug; } set { mDebug = value; } }

        static public void Initialize()
        {
            dummyTexture = new Texture2D(Engine.game.GraphicsDevice, 256, 256);

            Color[] lColor = new Color[256*256];

            for (int i = 0; i < 256; i++)
            {
                for (int j = 0; j < 256; j++)
                {
                    if (i < 8 || i > 256 - 8 || j < 8 || j > 256 - 8)
                        lColor[i * 256 + j] = new Color(1.0f, 1.0f, 1.0f, 1.0f);
                    else
                        lColor[i * 256 + j] = new Color(0.0f, 0.0f, 0.0f, 0.0f);
                }
            }

            dummyTexture.SetData<Color>(lColor);

            numberOfSpriteDraw = 0;
            debug = true;

            font = Engine.internalContentManager.Load<SpriteFont>("DefaultFont");
        }

        static public void DrawRectangle(Rectangle pRect)
        {
            DrawRectangle(pRect, Color.White);
        }

        /// <summary>
        /// Draw a semi transparent rectangle on the scene.
        /// </summary>
        /// <param name="pRect"></param>
        static public void DrawRectangle(Rectangle pRect, Color pColor)
        {
            if (!debug)
                return;

            Rectangle lRect = new Rectangle(pRect.X - (int)Components.Camera.mainCamera.upperCorner.X, pRect.Y - (int)Components.Camera.mainCamera.upperCorner.Y, pRect.Width, pRect.Height);

            Random lrand = new Random();
            Color lrandColo = new Color((float)lrand.NextDouble(), (float)lrand.NextDouble(), (float)lrand.NextDouble(), 1.0f);
            Engine.spriteBatch.Draw(dummyTexture, lRect, pColor);
        }

        static public void clearDebugLog()
        {
            if (debug)
            {
                
            }
        }

        static public void outputDebug(string pMessage)
        {
            if (debug)
            {
                Engine.spriteBatch.Begin();
                Engine.spriteBatch.DrawString(font, pMessage, Vector2.Zero, Color.Red);
                Engine.spriteBatch.End();
            }
        }
    }
}
